package com.voicechat.firstmet.activity;

import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.IBinder;
import android.provider.Settings;
import android.support.annotation.RequiresApi;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.voicechat.firstmet.R;
import com.voicechat.firstmet.base.BaseActivity;
import com.voicechat.firstmet.service.FloatVideoWindowService;
import com.voicechat.firstmet.utils.StatusBarUtils;
import com.voicechat.firstmet.view.RippleView;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @ProjectName: FirstMet
 * @Package: com.voicechat.firstmet.activity_imessage
 * @ClassName: VoiceCommActivity
 * @Description: 一键语音通话页面
 * @Author: ljp
 * @CreateDate: 2018/9/5 13:40
 * @Version: 1.0
 */
public class VoiceCommActivity extends BaseActivity implements View.OnClickListener {
    @BindView(R.id.linear_back)
    LinearLayout linearBack;
    @BindView(R.id.image_windows)
    ImageView imageWindows;
    @BindView(R.id.tv_wait_pops)
    TextView tvWaitPops;
    @BindView(R.id.tv_wait_time)
    TextView tvWaitTime;
    @BindView(R.id.linear_quick)
    RelativeLayout linearQuick;
    @BindView(R.id.rippleView)
    RippleView rippleView;
    private ServiceConnection mVideoServiceConnection;
    private String TAG = "VoiceCommActivity";

    @Override
    protected int setStatusBarColor() {
        return R.color.colorWhite;
    }

    @Override
    protected boolean isSettingStatusBarText() {
        return false;
    }

    @Override
    protected int loadLayout() {
        return R.layout.activity_voicecomm;
    }

    @Override
    protected void initLayout(Bundle savedInstanceState) {
        ButterKnife.bind(this);
        //透明状态栏
        StatusBarUtils.Full(this);
        mVideoServiceConnection = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                // 获取服务的操作对象
                FloatVideoWindowService.MyBinder binder = (FloatVideoWindowService.MyBinder) service;
                binder.getService();
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {
            }
        };
        rippleView.startRippleAnimation();
        //mRippleView.stopRippleAnimation();//结束动画
        rippleView.setAnimationProgressListener(new RippleView.AnimationListener() {
            @Override
            public void startAnimation() {
                //开始动画了
            }

            @Override
            public void EndAnimation() {
                //结束动画了
            }
        });

        linearBack.setOnClickListener(this);
        imageWindows.setOnClickListener(this);
        linearQuick.setOnClickListener(this);

    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //返回
            case R.id.linear_back:
                break;
            //缩放窗口
            case R.id.image_windows:

                break;
            //快速通道
            case R.id.linear_quick:
                startActivity(new Intent(this,VoiceChatingActivity.class));
                break;
        }

    }

    @Override
    public boolean moveTaskToBack(boolean nonRoot) {
        return super.moveTaskToBack(nonRoot);
    }

    public void startVideoService() {
        moveTaskToBack(true);//最小化Activity
        Intent intent = new Intent(this, FloatVideoWindowService.class);//开启服务显示悬浮框
        bindService(intent, mVideoServiceConnection, Context.BIND_AUTO_CREATE);

    }

    private static final int REQUEST_CODE = 1;

    private void requestAlertWindowPermission() {
        Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
        intent.setData(Uri.parse("package:" + getPackageName()));
        startActivityForResult(intent, REQUEST_CODE);
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE) {
            if (Settings.canDrawOverlays(this)) {
                startVideoService();
                Log.i("onActivityResult", "onActivityResult granted");
            }
        }
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        if(isServiceRunning(this,"com.voicechat.firstmet.service.FloatVideoWindowService")){
            unbindService(mVideoServiceConnection);//不显示悬浮框
            Log.i(TAG, "onRestart: -----onRestart---");
        }
        //从悬浮窗进来后重新设置画布(判断是不是接通了)
        /*if (isCallEstablished) {
            //如果接通，先清除所有画布
            avChatUI.clearAllSurfaceView(avChatUI.getAccount());
            //延迟重新加载远端和本地的视频画布
            mHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    avChatUI.initAllSurfaceView(avChatUI.getAccount());
                }
            }, 800);
        } else {
            //如果没接通，直接初始化所有画布
            avChatUI.initLargeSurfaceView(IMCache.getAccount());
        }*/
    }

    /**
     * 判断服务是否开启
     *
     * @return
     */
    public static boolean isServiceRunning(Context context, String ServiceName) {
        if (("").equals(ServiceName) || ServiceName == null)
            return false;
        ActivityManager myManager = (ActivityManager) context
                .getSystemService(Context.ACTIVITY_SERVICE);
        ArrayList<ActivityManager.RunningServiceInfo> runningService = (ArrayList<ActivityManager.RunningServiceInfo>) myManager
                .getRunningServices(30);
        for (int i = 0; i < runningService.size(); i++) {
            if (runningService.get(i).service.getClassName().toString()
                    .equals(ServiceName)) {
                return true;
            }
        }
        return false;
    }

}
